package com.tebiecloud.yunst.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.yunst.client.model.entity.YunStUser;
import com.tebiecloud.yunst.server.service.YunStUserService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 云商通用户表 前端控制器
 * @author tebie
 * @date 2019-09-17
 */
@RestController
@RequestMapping("/yunstuser")
public class YunStUserController {

    @Autowired
    private YunStUserService yunStUserService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<YunStUser>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        YunStUser query = pageParams.mapToObject(YunStUser.class);
        QueryWrapper<YunStUser> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(yunStUserService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<YunStUser> get(@RequestParam("id") Long id) {
        YunStUser entity = yunStUserService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param userId 内部使用的用户id,
     * @param bizUserId 通联用户id,
     * @param memberType 用户类型：2企业，3个人,
     * @param source 用户来源：1手机终端，2pc终端,
     * @param phone 绑定手机号,
     * @param name 真实姓名,
     * @param identityNo 身份证号,
     * @param acct 用户标识：绑定的微信openid或者支付宝的openid,
     * @param signContract 是否签约：0没有，1有
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "bizUserId", required = false) String bizUserId,
        @RequestParam(value = "memberType", required = false) Integer memberType,
        @RequestParam(value = "source", required = false) Integer source,
        @RequestParam(value = "phone", required = false) String phone,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "identityNo", required = false) String identityNo,
        @RequestParam(value = "acct", required = false) String acct,
        @RequestParam(value = "signContract", required = false) Integer signContract
    ) {
        YunStUser entity = new YunStUser();
        entity.setUserId(userId);
        entity.setBizUserId(bizUserId);
        entity.setMemberType(memberType);
        entity.setSource(source);
        entity.setPhone(phone);
        entity.setName(name);
        entity.setIdentityNo(identityNo);
        entity.setAcct(acct);
        entity.setSignContract(signContract);
        yunStUserService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param userId 内部使用的用户id
     * @param bizUserId 通联用户id
     * @param memberType 用户类型：2企业，3个人
     * @param source 用户来源：1手机终端，2pc终端
     * @param phone 绑定手机号
     * @param name 真实姓名
     * @param identityNo 身份证号
     * @param acct 用户标识：绑定的微信openid或者支付宝的openid
     * @param signContract 是否签约：0没有，1有
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "yunStUserId", required = false) Long yunStUserId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "bizUserId", required = false) String bizUserId,
            @RequestParam(value = "memberType", required = false) Integer memberType,
            @RequestParam(value = "source", required = false) Integer source,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "identityNo", required = false) String identityNo,
            @RequestParam(value = "acct", required = false) String acct,
            @RequestParam(value = "signContract", required = false) Integer signContract
    ) {
        YunStUser entity = new YunStUser();
        entity.setYunStUserId(yunStUserId);
        entity.setUserId(userId);
        entity.setBizUserId(bizUserId);
        entity.setMemberType(memberType);
        entity.setSource(source);
        entity.setPhone(phone);
        entity.setName(name);
        entity.setIdentityNo(identityNo);
        entity.setAcct(acct);
        entity.setSignContract(signContract);
        yunStUserService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            yunStUserService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            yunStUserService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
